package cn.hrc.controller;

import cn.hrc.dao.CouDao;
import cn.hrc.entity.Course;
import cn.hrc.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/adm/cou")
public class AdmCouController {
    @Autowired
    CouDao dao;
    @RequestMapping("/show")
    public String clzShow(String page, Model model, HttpSession session,
                          String cno){
        User who = (User)session.getAttribute("who");
        if(who == null){
            model.addAttribute("loginmsg","请先登录在调用该功能");
            //redirect:
            return "/login";
        }
        //页面总长度
        int sql = dao.countCou();
        Map<String,Integer> map = new HashMap<>();
        //前端传输过来的当前页
        System.out.println("当前页："+page);
        //每页6行【页面行】
        Integer pageLines = 6;
        //总页面
        Integer totalPages = sql/pageLines;
        System.out.println("总页面："+totalPages);
        //当前页初始化
        Integer current = 0;
        //首尾判断
        if (page != null){
            //有数据转换类型
            Integer page2 = Integer.valueOf(page);
            //上一页小于0，会一直在首页
            //下一页大于总页数会自动回到首页
            if (page2 < 0 || page2 >= totalPages){
                System.out.println("超出范围。。。。");
            }else {
                current = page2;
            }
        }
        //当前页进行上一页减一
        //开头显示第几个【判断，首页为0不能减一  因为他是从0开始的】
        Integer min = current*pageLines;
        System.out.println("min："+min);
        map.put("min",min);
        map.put("max",pageLines);
        //当前页面显示的数据
        List<Course> couList = dao.findByLimit(map);
        System.out.println("current："+current);
        Course cou ;
        //功能平常保持为添加
        if (cno == null){
            model.addAttribute("function","班级添加");
            model.addAttribute("method","add");
            cou = new Course();
        }else {
            cou = dao.findByCno(cno);
            model.addAttribute("function","班级更改");
            model.addAttribute("method","update");
        }
        model.addAttribute("cou",cou);
        System.out.println(cno);
        model.addAttribute("current",current);
        model.addAttribute("courses",couList);
        return "adm/couShow";
    }
    //传输添加的数据
    @RequestMapping("/add")
    public String intoClz(Course cou,String page,
                          Model model, HttpSession session){
        System.out.println("当前页："+page);
        System.out.println(cou);
        User who = (User)session.getAttribute("who");
        if(who == null){
            model.addAttribute("loginmsg","请先登录在调用该功能");
            //redirect:
            return "/login";
        }
        int num = dao.addCou(cou);
        return "forward:/adm/cou/show";
    }
    /** 去更改功能 */
    @GetMapping("/go2update/{cno}/{page}")
    public String updateClz(@PathVariable("cno")String cno,
                            Model model,HttpSession session,
                            @PathVariable("page") String page){
        System.out.println("去更改功能。。。。");
        System.out.println("当前页："+page+"\tclzno："+cno);
        User who = (User)session.getAttribute("who");
        if(who == null){
            model.addAttribute("loginmsg","请先登录在调用该功能");
            //redirect:
            return "/login";
        }
        return "forward:/adm/cou/show?cno="+cno+"&page="+page;
    }

    @PostMapping("/update")
    public String updateClz2(Course cou,String page,
                             Model model,HttpSession session){
        System.out.println("更改功能。。。。");
        System.out.println("当前页："+page+"\ncou："+cou);
        User who = (User)session.getAttribute("who");
        if(who == null){
            model.addAttribute("loginmsg","请先登录在调用该功能");
            //redirect:
            return "/login";
        }
        int sql = dao.updateCou(cou);
        System.out.println(sql);
        //跳转到显示 ，clz还有数据 这里直接给他为null
        model.addAttribute("cou",null);
        return "forward:/adm/cou/show";
    }

    @GetMapping("/delete/{cno}/{page}")
    public String deleteClz(@PathVariable("cno")String cno,
                            @PathVariable("page")String page){
        System.out.println("删除功能。。。。");
        System.out.println("当前页："+page+"\tcno："+cno);
        int sql = dao.deleteCou(cno);
        System.out.println(sql);
        return "forward:/adm/cou/show?page="+page;
    }
}
